unit UPNGImage;

interface
uses
  Windows,Messages,Classes,Graphics,ExtCtrls;

type
  TPNGImage=class(TImage)
  private
    FDrawing: Boolean;
    FPNGFile: string;
    m_pGPStart:Pointer;
    m_nToken:Integer;
    procedure SetPNGFile(const Value: string);

  protected
    procedure Paint;override;
  public
    constructor Create(AOwner:TComponent);override;
    destructor Destroy;override;
  published
    property PNGFile:string read FPNGFile write SetPNGFile;
  end;
  

procedure Register;

implementation
procedure Register;
begin
  RegisterComponents('aLoNe',[TPNGImage]);
end;

type
  Long=Integer;
type
 GdiplusStartupInput=record
   GdiplusVersion,
   DebugEventCallback,
   SuppressBackgroundThread,
   SuppressExternalCodecs: Long
 end;
 PGdiplusStartupInput=^GdiplusStartupInput;
   
function GdiplusStartup (var token:Long; var inputbuf: GdiplusStartupInput;outputbuf: Long):Integer;stdcall;external 'gdiplus.dll';
function GdiplusShutdown ( token:long):Integer;stdcall;external 'gdiplus.dll';
function GdipCreateFromHDC ( hDC:long;var Graphics:long):long;stdcall;external 'gdiplus.dll';
function GdipCreateFromHWND ( hwnd:long;var Graphics:long):long;stdcall;external 'gdiplus.dll';
function GdipDeleteGraphics ( Graphics:long):long;stdcall;external 'gdiplus.dll';
function GdipLoadImageFromFile ( FileName:long;var Image:long):long;stdcall;external 'gdiplus.dll';
function GdipDisposeImage ( Image:long):long;stdcall;external 'gdiplus.dll';
function GdipDrawImageI(Graphics:long;Image:long;x:long;y:long):Integer;stdcall;external 'gdiplus.dll';
function GdipDrawImageRectI(Graphics:long;Image:long;x:long;y:long;Width:long;Height:long):Integer;stdcall;external 'gdiplus.dll';

 
{ TPNGImage }

function drawimg (hdcThis:HDC; sImgFile:string;x, y,cx,cy:Integer): Boolean;stdcall
var
  pImg,pGraph:Integer;
  psImgFile:PWideChar;
begin
  psImgFile:=PWideChar( widestring(sImgFile));
  Result:=false;
  GdipCreateFromHDC(hdcThis,pGraph);
  GdipLoadImageFromFile(Integer( psImgFile),pImg);
  GdipDrawImageRectI(pGraph,pImg,x,y,cx,cy);
  GdipDeleteGraphics(pGraph);
  GdipDisposeImage(pImg);
  Result:=True;
end;  

constructor TPNGImage.Create(AOwner: TComponent);
var
  pGdip:PGdiplusStartupInput;
  dwOut:Integer;
begin
  inherited;
  new(pGdip);pGdip^.GdiplusVersion:=1;pGdip^.DebugEventCallback:=0;pGdip^.SuppressBackgroundThread:=0;pgdip^.SuppressExternalCodecs:=0;
  GdiplusStartup(m_nToken,pgdip^,dwOut);
  m_pGPStart:=pGdip;
end;

destructor TPNGImage.Destroy;
var
  pGdip:PGdiplusStartupInput;
begin
  pGdip:=m_pGPStart;
  GdiplusShutdown(m_nToken);
  Dispose(pGdip);
  inherited;
end;

procedure TPNGImage.Paint;
var
  hdcThis:HDC;
  x,y,cx,cy:Integer;
  Save:Boolean;
begin
  inherited;
  try
	with inherited Canvas do
  begin
    x:=0;y:=0;
    cx:=Width;cy:=Height;
    hdcThis:=Canvas.Handle;
    drawimg(hdcThis, FPNGFile ,x,y,cx,cy);
  end;
	  //StretchDraw(DestRect, Picture.Graphic);
  finally

  end;

end;

procedure TPNGImage.SetPNGFile(const Value: string);
begin
  FPNGFile := Value;
end;

end.
